package com.zeratul.cloud.demo2.controller.inner;

import com.zeratul.cloud.demo2.api.RemoteUserApi;
import com.zeratul.cloud.demo2.api.domain.bo.UserQueryBo;
import com.zeratul.cloud.demo2.api.domain.vo.UserVo;
import com.zeratul.cloud.demo2.domain.User;
import com.zeratul.common.core.utils.MapstructUtils;
import com.zeratul.common.satoken.utils.LoginHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.web.bind.annotation.RestController;

/**
 * 远程调用接口
 */
@RestController
@DubboService(async = true)
@Slf4j
public class RemoteUserController implements RemoteUserApi {

    @Override
    public UserVo getById(UserQueryBo userQueryBo) {
        log.info("【被调用方】远程调用执行，请求：{}，登录用户：{}", userQueryBo, LoginHelper.getLoginUser());
        log.info("【被调用方】登录用户：{}", LoginHelper.getLoginUser());
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        User users = switch (userQueryBo.getId()) {
            case 1 -> {
                User user = new User();
                user.setType(1);
                user.setUsername("张三");
                user.setPhone("13139981764");
                user.setPassword("123456");
                yield user;
            }
            case 2 -> {
                User user = new User();
                user.setType(2);
                user.setUsername("李四");
                user.setPhone("14522314123");
                user.setPassword("222222");
                yield user;
            }
            default -> null;
        };

        UserVo convert = MapstructUtils.convert(users, UserVo.class);
        log.info("【被调用方】转换的返回对象：{}", convert);
        return convert;
    }
}
